import React, { useState } from 'react';
import { message, Modal, Button } from 'antd';
import request from '@/utils/request';
import ScoreModal from './index';
import { modalTypes } from './modalTypes';
import styles from './index.less';

const RatingFormModal = ({ RatingFormModalRef, onSuccess }) => {
    const [modalType, setModalType] = useState(null);
    const [modalData, setModalData] = useState(null);
    const [modalVisible, setModalVisible] = useState(false);
    const [dimensionDataSource, setDimensionDataSource] = useState([]);
    const [informVisible, setInformVisible] = useState(false);
    const [informTitle, setInformTitle] = useState('');

    const showRatingModal = record => {
        setModalType(modalTypes.INITIAL_RATE.key);
        setModalData(record);
        setModalVisible(true);
    };
    const showReRatingModal = record => {
        setModalType(modalTypes.RE_RATE.key);
        setModalData(record);
        setModalVisible(true);
    };

    const handleGiveUpRate = record => {
        setModalType(modalTypes.GIVE_UP_RATE.key);
        setModalData(record);
        setModalVisible(true);
    };

    const checkScoreDimension = (record, isReRating = false, setDataLoading) => {
        // 判断时候有配置评分指标， 如果有则显示评分弹框，没有则弹框提示
        const getScoreDimension = async rec => {
            setDataLoading(true);
            const res = await request(`/api/oa/v1/work/task/score/${rec.taskScoreDetailId}/score_artificial_rules`);
            if (res.code === 10000) {
                if (res.data.length > 0) {
                    setDimensionDataSource(res.data);
                    if (isReRating) {
                        showReRatingModal(record);
                    } else {
                        showRatingModal(record);
                    }
                } else {
                    setInformTitle(isReRating ? '重评' : '评分');
                    setInformVisible(true);
                }
            } else {
                message.error(res.msg);
            }
            setDataLoading(false);
        };
        getScoreDimension(record);
    };

    const handleModalOk = async (values, setConfirmLoading, type, rowData) => {
        const { taskScoreDetailId, scoreState } = rowData;
        const { files, ...rest } = values;
        const paramsData = {
            ...rest,
            taskScoreDetailId,
            scoreState,
            resourceRelation: values.files ? [values.files.uploadResourceFile] : []
        };
        let res = {};
        if (type === modalTypes.INITIAL_RATE.key || type === modalTypes.RE_RATE.key) {
            if (paramsData.comprehensiveScore) {
                setConfirmLoading(true);
                res = await request('/api/oa/v1/work/task/score/actions/score', {
                    method: 'POST',
                    data: paramsData
                });
                setConfirmLoading(false);
            } else {
                return message.warning('请选择评分星级');
            }
        }
        if (type === modalTypes.GIVE_UP_RATE.key) {
            setConfirmLoading(true);
            res = await request('/api/oa/v1/work/task/score/actions/abandon', {
                method: 'PUT',
                data: paramsData
            });
            setConfirmLoading(false);
        }
        if (res.code === 10000) {
            message.success('提交成功');
            if (onSuccess) {
                onSuccess();
            }
            setModalVisible(false);
        } else {
            message.error(res.msg || '提交失败');
        }
        return null;
    };

    React.useImperativeHandle(RatingFormModalRef, () => ({
        checkScoreDimension,
        handleGiveUpRate
    }));

    return (
        <>
            <ScoreModal
                visible={modalVisible}
                type={modalType}
                data={modalData}
                modalTableData={dimensionDataSource}
                setVisible={setModalVisible}
                onOk={handleModalOk}
            />

            <Modal visible={informVisible} footer={null} onCancel={() => setInformVisible(false)} title={informTitle}>
                <div className={styles.informContent}>
                    <div className={styles.contentLine}>暂无评分指标，请联系系统管理人员前往</div>
                    <div className={styles.contentLine}>{'业务规则配置-> 人工评分规则配置'}</div>
                </div>
                <div className={styles.okButton}>
                    <Button type="primary" onClick={() => setInformVisible(false)}>
                        我知道了
                    </Button>
                </div>
            </Modal>
        </>
    );
};

export default RatingFormModal;
